using System;
using System.IO;

namespace SmartLauncher
{
	public enum StartModes
	{
		Interactive,
		Automated,
		SelfUpdate
	}

	/// <summary>
	/// This class represents the application starting context. Depending on the
	/// arguments received, it is considered as being started in automated mode
	/// or in interactive mode.
	/// </summary>
	static class ApplicationStartInfo
	{
		static StartModes _startMode;
		static string _targetApplication;
		static string _targetApplicationFolder;
		static string _localRoot;

		#region "Constructors"
		static ApplicationStartInfo()
		{
			string[] args;

			// === Check the start mode ============================================
			args = Environment.GetCommandLineArgs();
			_localRoot = Path.GetDirectoryName(args[0]).ToLower();

			if (args.Length == 1)
				_startMode = StartModes.Interactive;
			else
				//		_startMode = (ParseCommandLineArguments(args) == true ? StartModes.Automated : StartModes.Interactive);
				_startMode = (ParseCommandLineArguments(args) == true ? (_targetApplication.ToLower() == string.Format("/{0}", SelfUpdateEngine.PFX_UPDATE).ToLower() ? StartModes.SelfUpdate : StartModes.Automated) : StartModes.Interactive);
		}
		#endregion

		#region "Properties"
		public static StartModes StartMode
		{
			get { return _startMode; }
		}

		public static string TargetApplication
		{
			get { return _targetApplication; }
		}

		public static string TargetApplicationFolder
		{
			get { return _targetApplicationFolder; }
		}

		public static string LocalRoot
		{
			get { return _localRoot; }
		}
		#endregion

		#region "Methods"
		/// <summary>
		/// Verify commandline arguments
		/// </summary>
		/// <param name="args">
		/// Argmument array. There can be only to elements in the array:
		/// - args[0] : name and path of SmartLauncher
		/// - args[1] : name and path of the application that is started
		/// </param>
		/// <returns></returns>
		static bool ParseCommandLineArguments(string[] args)
		{
			if (args.Length < 2)
				return false; // TODO Show usage
			else
			{
				_targetApplication = args[1];
				_targetApplicationFolder = Path.GetDirectoryName(_targetApplication);
				return ((_targetApplication.ToLower() == string.Format("/{0}", SelfUpdateEngine.PFX_UPDATE).ToLower() ? true : File.Exists(_targetApplication)));
				/// TODO If the file does not exist, inform the user that the link is broken
			}
		}
		#endregion
	}
}
